package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.WxOrderBaseData;
import com.cskaoyan.bean.bo.WxOrderGoodsCommentBO;
import com.cskaoyan.bean.bo.WxOrderIdBO;
import com.cskaoyan.bean.bo.WxOrderListBO;
import com.cskaoyan.bean.bo.WxOrderSubmitBO;
import com.cskaoyan.bean.pojo.MallOrderGoods;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.WxOrderDataBeanVO;

import com.cskaoyan.bean.vo.WxOrderDetailVO;
import com.cskaoyan.service.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JJ
 * @description 订单管理
 * @time 2021/7/11 20:43
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;


    @RequestMapping("list")
    public BaseRespVo list(WxOrderListBO wxOrderListBO) {
        WxOrderBaseData<WxOrderDataBeanVO> listByWx = wxOrderService.getListByWx(wxOrderListBO);
        return BaseRespVo.ok(listByWx);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetailVO detailByWx = wxOrderService.getDetailByWx(orderId);
        return BaseRespVo.ok(detailByWx);
    }


    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody String orderId) {
        wxOrderService.prepayByWx(orderId);

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(724);
        baseRespVo.setErrmsg("订单不能支付");
        return baseRespVo;
    }


    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody WxOrderSubmitBO wxOrderSubmitBO) {
        Integer orderId = wxOrderService.submitByWx(wxOrderSubmitBO);

        if (orderId==-1){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(402);
            baseRespVo.setErrmsg("商品库存不足");
        }
        return BaseRespVo.ok(orderId);
    }


    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody WxOrderIdBO orderId) {
        Integer i = wxOrderService.cancelByWx(orderId);
        if (i <= 0) {
            return BaseRespVo.fail();
        }

        return BaseRespVo.ok();
    }


    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody WxOrderIdBO orderId) {
        Integer i = wxOrderService.refundByWx(orderId);
        if (i <= 0) {
            return BaseRespVo.fail();
        }

        return BaseRespVo.ok();
    }


    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WxOrderIdBO orderId) {
        Integer i = wxOrderService.deleteByWx(orderId);
        if (i <= 0) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody WxOrderIdBO orderId) {
        Integer i = wxOrderService.confirmByWx(orderId);
        if (i <= 0) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }


    @RequestMapping("goods")
    public BaseRespVo getGoods(Integer orderId,Integer goodsId) {
        MallOrderGoods goodsByWx = wxOrderService.getGoodsByWx(orderId, goodsId);

        return BaseRespVo.ok(goodsByWx);
    }


    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderGoodsCommentBO wxOrderGoodsCommentBO) {
        Integer i = wxOrderService.commentByWx(wxOrderGoodsCommentBO);
        if (i <= 0) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }


}
